﻿var bootstrap = function ($, vgole) {
    "use strict";
    var chartArr = [];
    //排序并设置权限
    function sortAndPower(loginInfo, data) {
        //数组排序
        var sortData = data.sort(function (a, b) {
            if (a.f_sortcode0 < b.f_sortcode0) return -1;
            if (a.f_sortcode0 > b.f_sortcode0) return 1;
            return 0;
        });
        //权限过滤
        var powerData = [];
        $.each(sortData, function (index, itemobj) {
            if (loginInfo.userId == "System") {
                powerData.push(itemobj);
            }
        });
        return powerData;
    }
    function loadDesktop() {
        //通知要求
        var htmlStr = "";
        htmlStr += '<div class="layui-col-md6 self" style="height:281px;">';
        htmlStr += '    <div class="portal-panel-title self-font">';
        htmlStr += '        <i class="layui-icon layui-icon-notice"></i>&nbsp;&nbsp;通知要求';
        htmlStr += '        <span id="noticemore" class="menu" title="更多"><span class="point"></span><span class="point"></span><span class="point"></span></span>';
        htmlStr += '    </div>';
        htmlStr += '    <div class="portal-panel-content" >';

        var handler = new HttpHandler("BP.PM.Handler.ProDemandDecHandler");
        handler.AddPara("EnName", "BP.PM.Policy.Notice");
        var res = handler.DoMethodReturnString("InitSortPolicyData");
        res = eval('(' + res + ')');

        if (res.Success && res.Data) {
            res = res.Data;
        }

        for (var j = 0; j < res.length; j++) {
            if (j > 4) continue;
            var item = res[j];
            htmlStr += '        <div class="vg-msg-line">';
            //if (typeof item.IsRead != undefined && item.IsRead == "0") {
            //    htmlStr += '<img src="images/mail_unread.png"/>';
            //}
            htmlStr += '<a id="' + item.OID +
                'item" href="javascript:window.top.vm.openTab(\'' + (item.Title.length > 13 ? item.Title.substr(0, 13) + "..." : item.Title) + '\', \'../App/zcfgcx/Utility/ListContent.html?id=' + item.OID + '&enName=BP.PM.Policy.Notice\');" title="' + item.Title +
                '" style="text-decoration: none;white-space: nowrap;">' + (item.Title.length > 13 ? item.Title.substr(0, 13) + "..." : item.Title) + '</a>';
            htmlStr += '                <label>' + item.ReleaseTime + '</label>';
            htmlStr += '        </div>';
        }
        htmlStr += '    </div>';
        htmlStr += '</div>';
        $("#MList").append(htmlStr);
        $("#noticemore").on('click', function () {
            window.top.vm.openTab("通知要求", "../App/zcfgcx/Utility/index.htm?enName=BP.PM.Policy.Notice");
        });

        //工作规范
        htmlStr = "";
        htmlStr += '<div class="layui-col-md6 self" style="height:281px;">';
        htmlStr += '    <div class="portal-panel-title self-font">';
        htmlStr += '        <i class="layui-icon layui-icon-chart-screen"></i>&nbsp;&nbsp;工作规范';
        htmlStr += '        <span id="jobspecificationmore" class="menu" title="更多"><span class="point"></span><span class="point"></span><span class="point"></span></span>';
        htmlStr += '    </div>';
        htmlStr += '    <div class="portal-panel-content" >';

        handler = new HttpHandler("BP.PM.Handler.ProDemandDecHandler");
        handler.AddPara("EnName", "BP.PM.Policy.JobSpecification");
        var res = handler.DoMethodReturnString("InitSortPolicyData");
        res = eval('(' + res + ')');

        if (res.Success && res.Data) {
            res = res.Data;
        }

        for (var j = 0; j < res.length; j++) {
            if (j > 4) continue;
            var item = res[j];
            htmlStr += '        <div class="vg-msg-line">';
            //if (typeof item.IsRead != undefined && item.IsRead == "0") {
            //    htmlStr += '<img src="images/mail_unread.png"/>';
            //}
            htmlStr += '<a id="' + item.OID +
                'item" href="javascript:window.top.vm.openTab(\'' + (item.Title.length > 13 ? item.Title.substr(0, 13) + "..." : item.Title) + '\', \'../App/zcfgcx/Utility/ListContent.html?id=' + item.OID + '&enName=BP.PM.Policy.JobSpecification\');" title="' + item.Title +
                '" style="text-decoration: none;white-space: nowrap;">' + (item.Title.length > 13 ? item.Title.substr(0, 13) + "..." : item.Title) + '</a>';
            htmlStr += '                <label>' + item.ReleaseTime + '</label>';
            htmlStr += '        </div>';
        }
        htmlStr += '    </div>';
        htmlStr += '</div>';
        $("#MList").append(htmlStr);
        $("#jobspecificationmore").on('click', function () {
            window.top.vm.openTab("工作规范", "../App/zcfgcx/Utility/index.htm?enName=BP.PM.Policy.JobSpecification");
        });

        //法规文件
        htmlStr = "";
        htmlStr += '<div class="layui-col-md6 self" style="height:281px;">';
        htmlStr += '    <div class="portal-panel-title self-font">';
        htmlStr += '        <i class="layui-icon layui-icon-template"></i>&nbsp;&nbsp;法规文件';
        htmlStr += '        <span id="regulationmore" class="menu" title="更多"><span class="point"></span><span class="point"></span><span class="point"></span></span>';
        htmlStr += '    </div>';
        htmlStr += '    <div class="portal-panel-content">';

        handler = new HttpHandler("BP.PM.Handler.ProDemandDecHandler");
        handler.AddPara("EnName", "BP.PM.Policy.Regulation");
        var res = handler.DoMethodReturnString("InitSortPolicyData");
        res = eval('(' + res + ')');

        if (res.Success && res.Data) {
            res = res.Data;
        }

        for (var j = 0; j < res.length; j++) {
            if (j > 4) continue;
            var item = res[j];
            htmlStr += '        <div class="vg-msg-line">';
            //if (typeof item.IsRead != undefined && item.IsRead == "0") {
            //    htmlStr += '<img src="images/mail_unread.png"/>';
            //}
            htmlStr += '<a id="' + item.OID +
                'item" href="javascript:window.top.vm.openTab(\'' + (item.Title.length > 13 ? item.Title.substr(0, 13) + "..." : item.Title) + '\', \'../App/zcfgcx/Utility/ListContent.html?id=' + item.OID + '&enName=BP.PM.Policy.Regulation\');" title="' + item.Title +
                '" style="text-decoration: none;white-space: nowrap;">' + (item.Title.length > 13 ? item.Title.substr(0, 13) + "..." : item.Title) + '</a>';
            htmlStr += '                <label>' + item.ReleaseTime + '</label>';
            htmlStr += '        </div>';
        }
        htmlStr += '    </div>';
        htmlStr += '</div>';
        $("#SeMList").append(htmlStr);
        $("#regulationmore").on('click', function () {
            window.top.vm.openTab("法规文件", "../App/zcfgcx/Utility/index.htm?enName=BP.PM.Policy.Regulation");
        });

        //技术资料
        htmlStr = "";
        htmlStr += '<div class="layui-col-md6 self" style="height:281px;">';
        htmlStr += '    <div class="portal-panel-title self-font">';
        htmlStr += '        <i class="layui-icon layui-icon-list"></i>&nbsp;&nbsp;技术资料';
        htmlStr += '        <span id="technicalmaterialmore" class="menu" title="更多"><span class="point"></span><span class="point"></span><span class="point"></span></span>';
        htmlStr += '    </div>';
        htmlStr += '    <div class="portal-panel-content" >';

        handler = new HttpHandler("BP.PM.Handler.ProDemandDecHandler");
        handler.AddPara("EnName", "BP.PM.Policy.TechnicalMaterial");
        var res = handler.DoMethodReturnString("InitSortPolicyData");
        res = eval('(' + res + ')');

        if (res.Success && res.Data) {
            res = res.Data;
        }

        for (var j = 0; j < res.length; j++) {
            if (j > 4) continue;
            var item = res[j];
            htmlStr += '        <div class="vg-msg-line">';
            //if (typeof item.IsRead != undefined && item.IsRead == "0") {
            //    htmlStr += '<img src="images/mail_unread.png"/>';
            //}
            htmlStr += '<a id="' + item.OID +
                'item" href="javascript:window.top.vm.openTab(\'' + (item.Title.length > 13 ? item.Title.substr(0, 13) + "..." : item.Title) + '\', \'../App/zcfgcx/Utility/ListContent.html?id=' + item.OID + '&enName=BP.PM.Policy.TechnicalMaterial\');" title="' + item.Title +
                '" style="text-decoration: none;white-space: nowrap;">' + (item.Title.length > 13 ? item.Title.substr(0, 13) + "..." : item.Title) + '</a>';
            htmlStr += '                <label>' + item.ReleaseTime + '</label>';
            htmlStr += '        </div>';
        }
        htmlStr += '    </div>';
        htmlStr += '</div>';
        $("#SeMList").append(htmlStr);
        $("#technicalmaterialmore").on('click', function () {
            window.top.vm.openTab("技术资料", "../App/zcfgcx/Utility/index.htm?enName=BP.PM.Policy.TechnicalMaterial");
        });

        return;
        //报表
        var par3 = {
            keyValue: '7b334cd7-8d8c-442f-b88f-838241ca83bb',//设计的桌面图表配置表单ID
            queryJson: '{ Enabled: "1" }'//获取有效的数据
        };

        vgole.httpAsync('GET', "DataHandler.ashx?method=" + "QS_FormModule_FormRelation_GetPreviewList_7b334cd7", par3, function (data) {
            data = [{
                "f_chartid0": "472c33c9-31b2-29ab-c966-847c5ce5e2d2", "f_name0": "各文件类型占比", "f_enabled0": 1, "f_icon0": "fa fa-pie-chart", "f_sortcode0": 1,
                "f_charttype0": "pie", "f_description0": null, "f_createdate0": "2020-05-05 01:12:43", "f_createuserid0": "System", "f_createusername0": "超级管理员111",
                "f_modifydate0": "2020-05-05 01:12:43", "f_modifyuserid0": "System", "f_modifyusername0": "超级管理员222", "f_databaselinkid0": "f2d587de-43e5-4310-b968-4544f4961a39",
                "f_sql0": "select case when SQL_WJLXT is null then '' ELSE SQL_WJLXT END name,count(SQL_WJLX) value from ND1Rpt group by SQL_WJLX"
            }, {
                "f_chartid0": "ede65916-19f3-2dfc-b580-4e69e11d9c17", "f_name0": "每月上报数量", "f_enabled0": 1, "f_icon0": "fa fa-line-chart", "f_sortcode0": 2,
                "f_charttype0": "line", "f_description0": null, "f_createdate0": "2020-05-05 01:12:36", "f_createuserid0": "System", "f_createusername0": "超级管理员",
                "f_modifydate0": "2020-05-05 01:12:36", "f_modifyuserid0": "System", "f_modifyusername0": "超级管理员", "f_databaselinkid0": "f2d587de-43e5-4310-b968-4544f4961a39",
                "f_sql0": "select case when FK_NY is null then '' ELSE FK_NY END name,count(FK_NY) value from ND1Rpt group by FK_NY"
            }];
            //vgole.httpAsync('GET', top.$.rootUrl + '/QS_FormModule/FormRelation/GetPreviewList', par3, function (data) {
            if (data) {
                var powerData = sortAndPower(loginInfo, data);
                $.each(powerData, function (index, itemobj) {
                    var htmlStr = "";
                    htmlStr += "<div class=\"col-xs-6 self\">";
                    htmlStr += "    <div class=\"portal-panel-title self-font\">";
                    htmlStr += "        <i class=\"" + itemobj.f_icon0 + "\"></i>&nbsp;&nbsp;" + itemobj.f_name0 + "";
                    htmlStr += "    </div>";
                    htmlStr += "    <div class=\"portal-panel-content\">";
                    htmlStr += "        <div id=\"" + itemobj.f_chartid0 + "chart\" class=\"vg-chart-container\"></div>";
                    htmlStr += "    </div>";
                    htmlStr += "</div>";
                    $("#Charts").append(htmlStr);
                    //绑定数值
                    var res = vgole.httpGet('DataHandler.ashx', { method: "GetSqlData", sql: itemobj.f_sql0 });

                    if (itemobj.f_chartid0 == '472c33c9-31b2-29ab-c966-847c5ce5e2d2') {
                        //res = { "code": 200, "info": "响应成功", "data": [{ "name": "装置性违章", "value": 9000 }, { "name": "管理性", "value": 0 }, { "name": "指挥性", "value": 102 }, { "name": "作业性", "value": 100900 }] };
                    }

                    if (itemobj.f_chartid0 == 'ede65916-19f3-2dfc-b580-4e69e11d9c17') {
                        //res = { "code": 200, "info": "响应成功", "data": [{ "name": "2019.07.13", "value": 9000 }, { "name": "2019.08.11", "value": 100900 }, { "name": "2019.09.25", "value": 0 }, { "name": "2020.04.03", "value": 100 }, { "name": "2020.05.21", "value": 2 }] };
                    }
                    if (itemobj.f_chartid0 == '9f87c7c7-2168-6cc7-dd38-36251c5ea0dd') {
                        //res = { "code": 200, "info": "响应成功", "data": [{ "name": "2017.07.13", "value": 9000 }, { "name": "2019.08.11", "value": 100900 }, { "name": "2017.09.25", "value": 0 }, { "name": "2018.04.03", "value": 100 }, { "name": "2018.05.21", "value": 2 }] };
                    }

                    if (res) {
                        var nameArr = [], valueArr = [];
                        $.each(res.data, function (index1, itemobj1) {
                            nameArr.push(itemobj1.name);
                            valueArr.push(itemobj1.value);
                        });
                        // 基于准备好的dom，初始化echarts实例$("#" + itemobj.f_chartid0 + "chart")
                        var chart = echarts.init(document.getElementById(itemobj.f_chartid0 + "chart"));
                        // 指定图表的配置项和数据
                        var option = {
                            legend: {
                                bottom: 'bottom',
                                data: nameArr
                            },
                            series: [
                                {
                                    name: itemobj.f_name0,
                                    type: itemobj.f_charttype0,
                                    data: res.data,
                                }
                            ]
                        };
                        switch (itemobj.f_charttype0) {
                            case "pie"://饼图
                                option.tooltip = {
                                    trigger: 'item',
                                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                                };
                                $.each(option.series, function (key, item) {
                                    item.radius = '75%';
                                    item.center = ['50%', '50%'];
                                    item.label = {
                                        normal: {
                                            formatter: '{b}:{c}: ({d}%)',
                                            textStyle: {
                                                fontWeight: 'normal',
                                                fontSize: 12,
                                                color: '#333'
                                            }
                                        }
                                    };
                                });
                                break;
                            case "line"://折线图
                                option.tooltip = {
                                    trigger: 'axis'
                                };
                                option.xAxis = {
                                    type: 'category',
                                    boundaryGap: false,
                                    data: nameArr
                                };
                                option.yAxis = {
                                    type: 'value'
                                };
                                break;
                            case "bar"://折线图
                                option.tooltip = {
                                    trigger: 'axis'
                                };
                                option.xAxis = {
                                    type: 'category',
                                    boundaryGap: false,
                                    data: nameArr
                                };
                                option.yAxis = {
                                    type: 'value'
                                };
                                break;
                            default:
                        }
                        // 使用刚指定的配置项和数据显示图表。
                        chart.setOption(option);
                        chartArr.push(chart);
                    }
                });
            }
        });
    }
    loadDesktop();
}